/**
 *  Document   : BookingManagement
 *  Created on : 17 juil. 2008, 16:54:37
 *  Author     : Rivo
 */
package gla.centralhotel.business;

import gla.centralhotel.business.interfaces.IResourceBrowser;
import gla.centralhotel.dao.interfaces.ICustomerDAO;
import gla.centralhotel.dao.interfaces.IRoomBookingDAO;
import gla.centralhotel.exception.CollectionRetrieverException;
import gla.centralhotel.exception.RoomBookingException;
import gla.centralhotel.model.Customer;
import gla.centralhotel.model.Hotel;
import gla.centralhotel.model.Room;
import gla.centralhotel.model.RoomBooking;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.ejb.Stateless;

@Stateless
public class BookingManagement implements IBookingManagement {

    @EJB
    private IRoomBookingDAO roomBookingDAO;
    @EJB
    private IResourceBrowser resourceBrowser;

    /**
     * 
     * @param roomBk
     * @param room
     * @return
     * @throws gla.centralhotel.exception.RoomBookingException
     */
//    public RoomBooking bookARoom(RoomBooking roomBk, Room room) throws RoomBookingException {
//        if (roomBk.getStartDate().before(roomBk.getEndDate()) || roomBk.getStartDate().equals(roomBk.getEndDate())) {
//            try {
//                ArrayList<RoomBookingHasRoom> roomsInBk = resourceBrowser.getAllRoomBookingHasRoomByRoom(room);
//                if (roomsInBk.size() >= 1) {
//                    ArrayList<RoomBooking> bookings = new ArrayList();
//                    Iterator<RoomBookingHasRoom> iter = roomsInBk.iterator();
//                    while (iter.hasNext()) {
//                        RoomBookingHasRoom rmBkHas = iter.next();
//                        bookings.add(rmBkHas.getRefBooking());
//                    }
//                    Iterator<RoomBooking> iterBookings = bookings.iterator();
//                    boolean isBusy = false;
//                    while (iterBookings.hasNext() && isBusy != true) {
//                        RoomBooking tmp = iterBookings.next();
//                        /* Params debut et fin
//                         Vérifie si les dates sont prises
//                         debut == start ET fin == end OU
//                         deb < start ET fin > end OU
//                         deb > start ET deb < end
//                         */
//                        if ((tmp.getStartDate().equals(roomBk.getStartDate()) && tmp.getEndDate().equals(roomBk.getEndDate())) || (roomBk.getStartDate().before(tmp.getStartDate()) && roomBk.getEndDate().after(tmp.getStartDate())) || (roomBk.getStartDate().after(tmp.getStartDate()) && roomBk.getStartDate().before(tmp.getEndDate()))) {
//                            isBusy = true;
//                        }
//                    }
//                    if (isBusy != true) {
//                        RoomBookingHasRoom rmBooking = new RoomBookingHasRoom();
//                        rmBooking.setRefBooking(roomBookingDAO.create(roomBk));
//                        rmBooking.setRefRoom(room);
//                    }
//                }
//            } catch (CollectionRetrieverException ex) {
//                Logger.getLogger(BookingManagement.class.getName()).log(Level.SEVERE, null, ex);
//            }
//        } else {
//            throw new RoomBookingException("Les dates de reservations sont incohérentes!");
//        }
//        return null;
//    }
    
    
    public ArrayList<RoomBooking> getRoomBookingByHotel(Integer hotelId)
    {
        
        try
        {
            return this.resourceBrowser.getAllRoomBookingByHotel(new Hotel(hotelId));
        }
        catch (CollectionRetrieverException ex)
        {
            Logger.getLogger(BookingManagement.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }
    
    public RoomBooking[] setTransientAttr(RoomBooking[] rbl)
    {
        for (int i = 0; i < rbl.length; i++)
        {
            RoomBooking rb = rbl[i];
            rbl[i] = this.setTransientAttr(rb);
        }
        return rbl;
    }
    
    /**
     * Remplit les attribut transiant
     * @param rb
     * @return
     */
    public RoomBooking setTransientAttr(RoomBooking rb)
    {
        rb.setAgencyName( rb.getRefAgency().getName() );
        rb.setCustomerFirstName( rb.getRefCustomer().getPrenom() );
        rb.setCustomerName( rb.getRefCustomer().getName() );
        rb.setHotelName( rb.getRefRoom().getRefHotel().getName());
        rb.setRoomNumber( rb.getRefRoom().getNumber() );
        rb.setRoomType( rb.getRefRoom().getRefRoomType().getType() );
        
        return rb;
    }
    
        /**
     * Remplit les attribut transiant
     * @param arlrb
     * @return
     */
    public ArrayList<RoomBooking> setTransientAttr(ArrayList<RoomBooking> arlrb)
    {
        for (int i = 0; i < arlrb.size(); i++)
        {
            RoomBooking rb = arlrb.get(i);
            arlrb.set(i, this.setTransientAttr(rb) );
        }
        return arlrb;
    }
    

    
}
